package com.axc.gallery2.edit;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.axc.gallery2.R;
import com.axc.gallery2.album.AlbumListViewActivity;
import com.axc.gallery2.edit.activity.MenuIcon;
import com.axc.gallery2.function.MultiImage;

public class EditScrawl extends RelativeLayout {

	Context context;
	// 头部
	FrameLayout frameLayout;
	// 头部菜单
	LinearLayout menu;
	// 放置 RenderView的容器在中间
	LinearLayout MyView;
	// 底部菜单
	LinearLayout bottom;
	LinearLayout bottomenu;
	RelativeLayout.LayoutParams bottomParams;

	ScrawlView scrawlView;

	// 底部菜单ImageView
	LinearLayout scrawlSave;
	LinearLayout scrawlReset;
	LinearLayout scrawlPenWidth;
	LinearLayout scrawlPen;
	LinearLayout scrawlPenStyle;
	LinearLayout scrawlColor;
	LinearLayout bgEraser;
	LinearLayout scrawlAbout;
	LinearLayout scrawlBack;
	LinearLayout home;
	// List<LinearLayout> bottomens = new ArrayList<LinearLayout>();

	// private int bottomMenuid = 1200;

	private int Pensize = 0;
	int shapeTyes = 0;
	int penstyle = 0;

	ScrawlAdapter scrawlAdapter = new ScrawlAdapter();

	ColorDialog coldialog;

	public EditScrawl(Context context, int w, int h) {
		super(context);
		this.setLayoutParams(new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.FILL_PARENT,
				RelativeLayout.LayoutParams.FILL_PARENT));
		Init(context, w, h);
	}

	public void Init(Context context, int w, int h) {
		this.context = context;
		// 头部
		frameLayout = new FrameLayout(context);
		RelativeLayout.LayoutParams frameParams = new RelativeLayout.LayoutParams(
				new RelativeLayout.LayoutParams(
						RelativeLayout.LayoutParams.FILL_PARENT,
						RelativeLayout.LayoutParams.WRAP_CONTENT));
		frameParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		// 中间
		MyView = new LinearLayout(context);
		scrawlView = new ScrawlView(context, w, h);

		RelativeLayout.LayoutParams myviewParams = new RelativeLayout.LayoutParams(
				w, h);// RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT));

		myviewParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
		myviewParams.addRule(RelativeLayout.CENTER_VERTICAL);

		MyView.addView(scrawlView);

		// 头部一个点菜单
		// menu = new MenuIcon(context, R.string.render_menu,
		// R.drawable.go_menu);
		// 头部添加点菜单
		// frameLayout.addView(menu);

		

		this.addView(MyView, myviewParams);
		// this.addView(frameLayout, frameParams);
		// 底部线程
		TBottom tBottom = new TBottom();
		tBottom.run();
	}

	/**
	 * 自动搜索ImageView<b>根据LinearLayout搜索子控件ImageView</b> <b>getImageView 关联
	 * CreateImageViewMenu</b>
	 * 
	 * @param needview
	 *            传入LinearLayout
	 * @param listener
	 *            传入监听事件
	 */
	public void CreateImageViewMenu(LinearLayout linearView, int id,
			OnClickListener listener) {
		linearView = (LinearLayout) findViewById(id);
		for (int i = 0; i < linearView.getChildCount(); i++) {
			View v = linearView.getChildAt(i);
			if (v instanceof LinearLayout) {
				LinearLayout linear = (LinearLayout) linearView.getChildAt(i);
				linear.setOnClickListener(listener);
				// getImageView(linear, listener);
			}
		}
	}

	public class TBottom implements Runnable {
		@Override
		public void run() {
			LinearLayout bottomContainer = new LinearLayout(context);
			bottomContainer.setBackgroundColor(R.color.transparent);
			HorizontalScrollView bottomScrollView = new HorizontalScrollView(
					context);
			// 底部
			bottom = new LinearLayout(context);
			bottomParams = new RelativeLayout.LayoutParams(
					new RelativeLayout.LayoutParams(
							RelativeLayout.LayoutParams.FILL_PARENT,
							RelativeLayout.LayoutParams.WRAP_CONTENT));
			// 添加其他属性
			bottomParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);// 至于容器的底部
			// 保存按钮
			addButtomMenu(context, scrawlSave, R.string.save,
					R.drawable.save_icon, penSaveClickListener);
			// 重设按钮
			addButtomMenu(context, scrawlReset, R.string.reset,
					R.drawable.refresh_icon, penResetClickListener);
			// 设置画笔按钮
			addButtomMenu(context, scrawlPenWidth, R.string.penwidth,
					R.drawable.penstroke, penWidthClickListener);
			// 设置画笔颜色按钮
			addButtomMenu(context, scrawlColor, R.string.scrawl_color,
					R.drawable.colorboard_icon, colorListener);

			// 图案画笔
			addButtomMenu(context, scrawlPen, R.string.apen,
					R.drawable.pen_icon, imgPenClickListener);

			// 画笔样式
			addButtomMenu(context, scrawlPenStyle, R.string.astyle,
					R.drawable.penstyle_icon, penStyleClickListener);

			// 背景橡皮擦
			addButtomMenu(context, bgEraser, R.string.aearser,
					R.drawable.earse_icon, earserListener);

			// 关于按钮
			addButtomMenu(context, scrawlAbout, R.string.render_about,
					R.drawable.about_icon, null);
			// 返回按钮
			addButtomMenu(context, scrawlBack, R.string.render_dissmiss,
					R.drawable.back_activity, backclickListener);
			
			addButtomMenu(context, home,R.string.homepage, R.drawable.home_activity, homeListener);

			// 底部菜单
			// bottomParams还未实例化
			bottomScrollView.addView(bottom, bottomParams);
			bottomContainer.addView(bottomScrollView);
			EditScrawl.this.addView(bottomContainer, bottomParams);
		}

		/**
		 * 添加菜单按钮
		 * 
		 * @param context
		 * @param linear
		 * @param string
		 * @param iconId
		 * @param listener
		 */
		public void addButtomMenu(Context context, LinearLayout linear,
				int string, int iconId, OnClickListener listener) {
			linear = new MenuIcon(context, string, iconId);
			bottom.addView(linear);
			linear.setOnClickListener(listener);
		}
	}

	/**
	 * 跳转到主页
	 */
	private OnClickListener homeListener=new OnClickListener() {
		
		@Override
		public void onClick(View v) { 
					Intent intent=new Intent();
					intent.setClass(ScrawlActivity.scrawl,AlbumListViewActivity.class);
					ScrawlActivity.scrawl.startActivity(intent);
		}
	};
	
	/**
	 *画笔样式：空心与实心
	 */
	private OnClickListener penStyleClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Dialog dialog = new AlertDialog.Builder(context).setTitle("画笔样式")
					.setSingleChoiceItems(new String[] { "实心", "空心" },
							penstyle, new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									switch (which) {
									case 0:
										scrawlAdapter
												.setPenfillStyle(scrawlView);
										penstyle = 0;
										break;
									case 1:
										scrawlAdapter
												.setPenHollowStyle(scrawlView);
										penstyle = 1;
										break;
									}
								}
							}).setPositiveButton("确定",
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									// TODO Auto-generated method stub
									dialog.dismiss();
								}
							}).create();
			dialog.show();
		}
	};

	/**
	 * 画笔样式
	 */
	private OnClickListener imgPenClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Dialog dialog = new AlertDialog.Builder(context).setTitle("橡皮擦")
					.setSingleChoiceItems(
							new String[] { "普通", "圆", "正方形", "三角形", "叶", "Single泡泡",
									"泡泡画笔", "梦幻画笔" }, shapeTyes,
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									// TODO Auto-generated method stub
									switch (which) {
									case 0:
										scrawlAdapter.imagePen(scrawlView,
												scrawlView, 0);
										shapeTyes = 0;
										break;
									case 1:
										scrawlAdapter.imagePen(scrawlView,
												scrawlView, 1);
										shapeTyes = 1;
										break;

									case 2:
										scrawlAdapter.imagePen(scrawlView,
												scrawlView, 2);
										shapeTyes = 2;
										break;

									case 3:
										scrawlAdapter.imagePen(scrawlView,
												scrawlView, 3);
										shapeTyes = 3;
										break;
									// 4开始自定义画笔
									case 4:
										scrawlAdapter.CustomerPen(scrawlView,
												R.drawable.leaf);
										shapeTyes = 4;
										break;
									case 5:
										scrawlAdapter.MilPao(scrawlView,
												new MultiImage().popo, 90,
												90, 1);
										shapeTyes = 5;
										break;
									case 6:
										scrawlAdapter.MilPao(scrawlView,
												new MultiImage().Paos, 138,
												138, 3);
										shapeTyes = 6;
										break;
									case 7:
										scrawlAdapter.MilPao(scrawlView,
												new MultiImage().Circles, 90,
												90, 1);
										shapeTyes = 7;
										break;
									}
								}
							}).setPositiveButton("确定",
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									// TODO Auto-generated method stub
									dialog.dismiss();
								}
							}).create();
			dialog.show();
		}
	};

	/**
	 * 橡皮擦
	 */
	private OnClickListener earserListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			scrawlAdapter.Eraser(scrawlView);
		}
	};

	/**
	 * 关闭进程
	 */
	private OnClickListener backclickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// 因为不可以传参数到Activity所以这里是用exit(0);退出窗口
			//System.exit(0);
			scrawlAdapter.endActivity(ScrawlActivity.scrawl);
		}
	};

	/**
	 * 底部颜色按钮监听事件
	 */
	private OnClickListener colorListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			coldialog = new ColorDialog(context, Color.YELLOW, "选择颜色",
					new ColorDialog.OnColorChangedListener() {
						@Override
						public void colorChanged(int color) {
							scrawlAdapter.setPenColor(scrawlView, color);
						}
					});
			coldialog.show();
		}
	};

	/**
	 * 底部保存按钮监听事件
	 */
	public OnClickListener penSaveClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			 new SaveBitmap().Save(context,ScrawlView.mBitmapSave);
		}
	};

	/**
	 * 底部画布重设按钮监听事件
	 */
	public OnClickListener penResetClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			scrawlAdapter.newCanvas(scrawlView);
		}
	};

	/**
	 * 底部画笔粗细按钮监听事件
	 */
	private OnClickListener penWidthClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// 设置画笔宽度 这里需要封装一下
			setPenWidth(v);
		}
	};

	/**
	 * 设置画笔粗度
	 * 
	 * @param v
	 *            按钮的View
	 */
	public void setPenWidth(View v) {
		v.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Dialog dialog = new AlertDialog.Builder(context).setTitle(
						"画笔大小")
						.setSingleChoiceItems(
								new String[] { "笔粗：1", "笔粗：5", "笔粗：9", "笔粗：13",
										"笔粗：17" }, Pensize,
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										// TODO
										// Auto-generated
										// method stub
										switch (which) {
										case 0:
											scrawlAdapter.setPenWidth(
													scrawlView, 1);
											Pensize = 0;
											break;
										case 1:
											scrawlAdapter.setPenWidth(
													scrawlView, 5);
											Pensize = 1;
											break;
										case 2:
											scrawlAdapter.setPenWidth(
													scrawlView, 9);
											Pensize = 2;
											break;
										case 3:
											scrawlAdapter.setPenWidth(
													scrawlView, 13);
											Pensize = 3;
											break;
										case 4:
											scrawlAdapter.setPenWidth(
													scrawlView, 17);
											Pensize = 4;
											break;
										default:
											break;
										}
									}
								}).setPositiveButton("确定",
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										dialog.dismiss();
									}
								}).create();
				dialog.show();
				dialog.show();
			}
		});
	} 
} 